<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Backend_model extends CI_Model
    {
        function __construct()
        {
            parent::__construct();
            $this->load->database();
        }
        
        /**
         * get data from table
         * if table has 'isdel' field then set isdel equal 1 or 0 
        */
        function get_data_from_table($table,$isdel=null)
        {
            $this->db->select('*');
            $this->db->from($table);
            if(isset($del)) $this->db->where('isdel',$isdel);
            $this->db->order_by('id','desc');
            $query=$this->db->get();
            return $query->result_array();
        }
        
        /**
         * change status of field
        */
        function change_status($table,$id,$field,$status)
        {
            $data=array(
                $field => $status
            );
            $this->db->where('id',$id);
            $this->db->update($table,$data);
        }
        
        /**
         * delete record of table
        */
        function delete_record($table,$id)
        {
            if(isset($id))
            {
                $this->db->where('id',$id);
                $this->db->delete($table);
            }
        }
        
        /**
         * get data of table by parentid 
        */
        function get_data_table_by_parentid($table,$parentid)
        {
            $this->db->select('*');
            $this->db->from($table);
            $this->db->where('parentid',$parentid); 
            //check table has 'isdelete' field, yes or no? if exists then get with isdelete equal 0
            if($this->db->field_exists('isdelete',$table))
            {
                $this->db->where('isdelete',0);
            }
            $this->db->order_by('sort asc,id asc');
            $query=$this->db->get();
            return $query->result_array();
        }
        
        /**
         * 
        */
        function insert_data_into_table($table,$list_field)
        {
            return $this->db->insert($table,$list_field);
        }
        
        /**
         * 
        */
        function update_data_into_table($table,$list_field)
        {
            $this->db->where('id',$list_field['id']);
            return $this->db->update($table,$list_field);
        }
        
        /**
         * get data of table by id 
        */
        function get_data_table_by_id($table,$id)
        {
            $this->db->select('*');
            $this->db->from($table);
            $this->db->where('id',$id); 
            $query=$this->db->get();
            return $query->row_array();
        }
    }
?>